package viper.diversity;

/**
 * @author Joe Coyne
 * 
 * Revision number: 3
 * Author: Joe Coyne
 * Date created: 10/25/09
 * Date of Last Back-up on SVN: 03/10/2010
 * 
 * <p>Diversity will take in either 2 or 3 voice packets depending on the scheme utilized and
 * then utilizing the correct algorithm turn them into new diversity packets to be sent over
 * the network 
 * 
 * Revision List:
 * 		- Was getting several array out of bounds exceptions which are now fixed; removed
 *        all of System.arraycopy in lieu of more elegant solution
 * 		- Revising Author: Joe Coyne
 * 		- Date Implemented: 3/10/2010
 */

public class Diversity{
	//All variables will be static in order that methods can be accessed statically
	// All variables used in class instantiated before any method declarations.
	
	static byte  [] A;
	static byte  [] B;
	static byte  [] C;
	static byte  [] A_Xor_B;
	static byte  [] A_Xor_C;
	static byte  [] B_Xor_C;
	static byte  [] A_Xor_B_temp;
	static byte  [] A_Xor_C_temp;
	static byte  [] B_Xor_C_temp;
	static byte  [][] Two_Three_x;
	static byte  [][] Four_Six_x;

	/*
	 * Method 1 which takes in two voice packets, XOR's them and then returns
	 * a 2D array with the original two packets and the 3rd new diversity packet
	 * 
	 */
	
	public static byte[][] diversify(byte [] one, byte [] two)
	{
		Two_Three_x = new byte [3][];
		A_Xor_B_temp = new byte [one.length];
		// The following for loop is a process by which all XOR operations will be performed
		// Creating a new Diversity packet
		
		for(int i=0;i<one.length;i++){
			A_Xor_B_temp [i]=(byte)(one[i]^two[i]);
					}
		Two_Three_x[0]=one;
		Two_Three_x[1]=two;
		Two_Three_x[2]=A_Xor_B_temp;
		return Two_Three_x;
	}
	/*
	 * Method 2 which overloads method 1, takes in 3 voice packets, XOR's them and then returns
	 * a 2D array with the original three packets and the 3 new diversity packets
	 * 
	 */
	
	public static byte [][] diversify(byte [] one, byte [] two, byte [] three){
		Four_Six_x = new byte [6][];
		A_Xor_B_temp = new byte [one.length];
		A_Xor_C_temp = new byte [two.length];
		B_Xor_C_temp = new byte [three.length];
		for(int i=0;i<one.length;i++){
			A_Xor_B_temp [i]=(byte)(one[i]^two[i]);
		}
		for(int i=0;i<two.length;i++){
			A_Xor_C_temp [i]=(byte)(one[i]^three[i]);
		}
		for(int i=0;i<two.length;i++){
			B_Xor_C_temp [i]=(byte)(two[i]^three[i]);
		}
		Four_Six_x[0]=one;
		Four_Six_x[1]=two;
		Four_Six_x[2]=three;
		Four_Six_x[3]=A_Xor_B_temp;
		Four_Six_x[4]=A_Xor_C_temp;
		Four_Six_x[5]=B_Xor_C_temp;
		return Four_Six_x;	
	}

}


